<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<!--
Copyright 2008 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<title>China Syndrome</title>
<!-- *** Replace the key below below with your own API key, available at http://code.google.com/apis/maps/signup.html *** -->
  <script src="http://www.google.com/jsapi?key=ABQIAAAAwbkbZLyhsmTCWXbTcjbgbRSzHs7K5SvaUdm8ua-Xxy_-2dYwMxQMhnagaawTo7L1FE1-amhuQxIlXw"></script>
  <script type="text/javascript">
google.load("earth", "1");
google.load("maps", "2");

var geMaster, geSlave;
var geocoder;
var laMaster, laSlave;
var camMaster, camSlave;

function fitLongitude(lon) {
  while (lon < 0)
    lon += 360;

  lon = lon % 360;

  return (lon > 180) ? lon - 360 : lon;
}

function el(e) { return document.getElementById(e); }
function syncSlave() {
  if (geMaster && geSlave && laMaster && laSlave) {
    if (el("china").checked) {
      laMaster = geMaster.getView().copyAsLookAt(geMaster.ALTITUDE_RELATIVE_TO_GROUND);
      laSlave.set(-laMaster.getLatitude(), fitLongitude(laMaster.getLongitude() + 180),
                  laMaster.getAltitude(), laMaster.getAltitudeMode(), 
                  laMaster.getHeading(), laMaster.getTilt(),
                  laMaster.getRange());
      geSlave.getView().setAbstractView(laSlave);            
    } else if (el("eyes").checked) {
      camMaster = geMaster.getView().copyAsCamera(geMaster.ALTITUDE_RELATIVE_TO_GROUND);
      camSlave.set(camMaster.getLatitude(), camMaster.getLongitude(),
                  camMaster.getAltitude(), camMaster.getAltitudeMode(), 
                  camMaster.getHeading() + 180, camMaster.getTilt(), 
                  camMaster.getRoll());
      geSlave.getView().setAbstractView(camSlave);
    }
  }
}

function init() {
  geocoder = new google.maps.ClientGeocoder();

  // Create the earths
  var syncActive = false;
  google.earth.createInstance(
    "geMaster",
    function(object) {
      geMaster = object;
      geMaster.getLayerRoot().enableLayerById(geMaster.LAYER_BORDERS, true);
      geMaster.getLayerRoot().enableLayerById(geMaster.LAYER_BUILDINGS, true);
      geMaster.getOptions().setAtmosphereVisibility(true);
      laMaster = geMaster.createLookAt('');
      camMaster = geMaster.createCamera('');
      syncSlave();
      if (!syncActive) {
        google.earth.addEventListener(geMaster.getView(), 'viewchange', syncSlave);
        syncActive = true;
      }
      geMaster.getWindow().setVisibility(true);
    },
    function(object) {});
  google.earth.createInstance(
    "geSlave",
    function(object) {
      geSlave = object;
      geSlave.getLayerRoot().enableLayerById(geSlave.LAYER_BORDERS, true);
      geSlave.getLayerRoot().enableLayerById(geSlave.LAYER_BUILDINGS, true);
      geSlave.getOptions().setAtmosphereVisibility(true);
      geSlave.getOptions().setFlyToSpeed(geSlave.SPEED_TELEPORT);
      geSlave.getOptions().setMouseNavigationEnabled(false);
      laSlave = geSlave.createLookAt('');
      camSlave = geSlave.createCamera('');
      syncSlave();
      if (!syncActive && geMaster) {
        google.earth.addEventListener(geMaster.getView(), 'viewchange', syncSlave);
        syncActive = true;
      }
      geSlave.getWindow().setVisibility(true);
    },
    /***
     * This function will be called if plugin fails to load, in case
     * you need to handle that error condition.
     ***/
    function(object) {});
}

function submitLocation() {
  var address = el('address').value;
  geocoder.getLatLng(
    address, 
    function(point) {
      if (point && geMaster != null)
        var la = geMaster.createLookAt('');
        la.set(point.y, point.x, 0, geMaster.ALTITUDE_RELATIVE_TO_GROUND, 
               100, 0, 1000000);
        geMaster.getView().setAbstractView(la);
    }
  );
}

</script>

</head>
<body onload='init()' id='body'>
  <table>
    <tr>
      <td>
        <form action='javascript:submitLocation();void(0);'>
          <input type=text size=40 id='address'></input>
          <input type=submit value='Go to location'>
        </form>
      </td>
      <td>&nbsp;</td>
      <td>
        <form>
          <input type="radio" id="china" name="slaveMode" onclick="syncSlave()" checked>China Syndrome</input>
          <br />
          <input type="radio" id="eyes" name="slaveMode" onclick="syncSlave()">Eyes in the back of your head</input>
        </form>
      </td>
    </tr>
  </table>

  <table>
    <tr>
      <td width=600>
        <div id='geMasterContainer' style='border: 1px solid silver; height: 600px;'>
          <div id='geMaster' style='height: 100%;'></div>
        </div>
      </td>
      <td width=600>
        <div id='geSlaveContainer' style='border: 1px solid silver; height: 600px;'>
          <div id='geSlave' style='height: 100%;'></div>
        </div>
      </td>
    </tr>
  </table>
</body>
</html>
